/*D6qijQE+tseVhRgdMUhc8HIHp+Zap115AM1KwOk7mYy6WCFeR8YjTFchq6I6n+MZ*/
/*
 * @Author: xiahui 
 * @PageInfo: 删除 
 * @Date: 2019-01-15 14:43:19 
 * @Last Modified by: xiahui
 * @Last Modified time: 2019-07-29 08:44:33
 */
import { ajax } from 'nc-lightapp-front';
import { AREA, URL, FIELDS } from '../../constance';
import {
	showErrorInfo,
	showSuccessInfo,
	showWarningInfo,
	showBatchOperateInfo,
	showDeleteDialog
} from '../../../../../scmpub/scmpub/pub/tool/messageUtil';
import { deleteCacheDataForList } from '../../../../../scmpub/scmpub/pub/cache/cacheDataManager';
import { getLangByResId } from '../../../../../scmpub/scmpub/pub/tool/multiLangUtil';
import { buttonControl } from '../viewControl/buttonControl';

export default function deleteBtnClick(props, record, index) {
	if (record && index >= 0) {
		// 行操作删除
		deleteOperation.call(this, props, record, index);
	} else {
		// 按钮删除
		showDeleteDialog({
			beSureBtnClick: () => {
				deleteOperation.call(this, props);
			}
		});
	}
}

function deleteOperation(props, record, index) {
	let checkArr = [];
	let reqData = { infos: [] };
	if (record && index >= 0) {
		// 行删除
		reqData.infos.push({
			id: record[FIELDS.pk_ct_pu].value,
			ts: record[FIELDS.ts].value
		});
	} else {
		// 按钮删除
		checkArr = props.table.getCheckedRows(AREA.listTableId);
		if (!checkArr || checkArr.length < 1) {
			showWarningInfo(null, getLangByResId(this, '4004PURDAILY-000058')); /* 国际化处理： 请选择数据*/
			return;
		}
		checkArr.forEach((row) => {
			reqData.infos.push({
				id: row.data.values[FIELDS.pk_ct_pu].value,
				ts: row.data.values[FIELDS.ts].value
			});
		});
	}

	ajax({
		url: URL.delete,
		data: reqData,
		success: (res) => {
			if (res.success && res.data) {
				let sucIds = res.data.successIds;
				let sucIndex = [];
				if (record && index >= 0) {
					sucIndex.push(index);
				} else {
					sucIds.forEach((sucId) => {
						for (var i = 0; i < checkArr.length; i++) {
							if (checkArr[i].data.values[FIELDS.pk_ct_pu].value === sucId) {
								sucIndex.push(checkArr[i].index);
								break;
							}
						}
					});
				}
				// 缓存删除
				deleteCacheDataForList(props, AREA.listTableId, sucIds);
				props.table.deleteTableRowsByIndex(AREA.listTableId, sucIndex);

				// 提示信息
				if (res.data.errMsg) {
					showBatchOperateInfo(null, res.data.message, res.data.errMsg);
				} else {
					showSuccessInfo(getLangByResId(this, '4004PURDAILY-000010')); /* 国际化处理： 删除成功*/
				}
				//更新按钮状态
				buttonControl.call(this, props, props.table.getCheckedRows(AREA.listTableId));
			} else {
				showErrorInfo(null, res.data.message);
			}
		}
	});
}

/*D6qijQE+tseVhRgdMUhc8HIHp+Zap115AM1KwOk7mYy6WCFeR8YjTFchq6I6n+MZ*/